﻿var zombies = zombies || {};

zombies.CategoryList = function () {
    var self = this;

    self.Loaded = ko.observable(false);
    self.Categories = ko.observableArray();

    self.CurrentPage = ko.observable(1);
    self.Pages = ko.observableArray();
    self.SearchString = ko.observable("");
    self.SearchText = ko.observable("");

    self.SearchCategory = function (viewModel, event) {
        self.SearchText(viewModel.SearchString());
        self.Categories.removeAll();
        self.Pages.removeAll();

        $.ajax({
            url: '/api/Recommend/GetCategoryList',
            type: 'POST',
            data: ko.toJSON({ SearchString: self.SearchText(), CurrentPage: 1 }),
            contentType: "application/json",
            dataType: "json"
        }).done(function (request) {
            for (i = 1; i <= request.totalPage && request.totalPage > 1; i++) {
                self.Pages.push(i);
            }
            $.each(request.list, function (idx, item) {
                self.Categories.push(new zombies.CategoryItem(item));
            });
        });
    };

    self.ChangePage = function (page) {
        self.Categories.removeAll();
        self.Pages.removeAll();

        $.ajax({
            url: '/api/Recommend/GetCategoryList',
            type: 'POST',
            data: ko.toJSON({ SearchString: self.SearchText(), CurrentPage: page }),
            contentType: "application/json",
            dataType: "json"
        }).done(function (request) {
            for (i = 1; i <= request.totalPage && request.totalPage > 1; i++) {
                self.Pages.push(i);
            }
            $.each(request.list, function (idx, item) {
                self.Categories.push(new zombies.CategoryItem(item));
            });
        });
    };
};

ko.bindingHandlers.enterKey = {
    init: function (element, valueAccessor, allBindings, vm) {
        ko.utils.registerEventHandler(element, "keyup", function (event) {
            if (event.keyCode === 13) {
                ko.utils.triggerEvent(element, "change");
                valueAccessor().call(vm, vm); //set "this" to the data and also pass it as first arg, in case function has "this" bound
            }

            return true;
        });
    }
};

zombies.CategoryItem = function (item) {
    var self = this;
    self.Link = 'CategoryListAdv/' + item.CatId;
    self.HtmlTag = "<img src='../" + item.IconPath + "' style='width: 150px; height: 150px;'><h6>" + item.Name + "</h6>";
}

var load = function () {
    $.ajax({
        url: '/api/Recommend/GetCategoryList',
        type: 'POST',
        data: ko.toJSON({ SearchString: "", CurrentPage: 1 }),
        contentType: "application/json",
        dataType: 'json'
    }).done(function (request) {
        for (i = 1; i <= request.totalPage && request.totalPage > 1; i++) {
            categoryListViewModel.Pages.push(i);
        }
        $.each(request.list, function (idx, item) {
            categoryListViewModel.Categories.push(new zombies.CategoryItem(item));
        });
    });
};

var categoryListViewModel;
$(document).ready(function () {
    categoryListViewModel = new zombies.CategoryList();    
    load();
    ko.applyBindings(categoryListViewModel, document.getElementById("my-container"));
});